
#include "string.h"
#include "button.h"

void button_scan_tick_handler(BUTTON_STRUCT* button, uint8_t tick)
{
	uint8_t button_state = BUTTON_RELEASE;
	if(button->hal_read_button_state_callbakc != 0){
		button_state = button->hal_read_button_state_callbakc(button);
		if(button_state == BUTTON_RELEASE){
			if(button->last_state == BUTTON_RELEASE){
				//button free
			}else{
				if(button->press_tick_count >= button->jitter_tick){
					if(button->release_callback != 0){
						button->release_callback(button);
					}
				}
			}
			button->press_tick_count = 0;
		}
		else{
			if(button->last_state == BUTTON_RELEASE){
				if(button->press_tick_count == 0){
					if(button->press_callback != 0){
						button->release_callback(button);
					}
				}
			}else{
				if(button->press_tick_count >= button->long_press_tick){
					if(button->long_press_callback != 0){
						button->long_press_callback(button);
					}
					button->press_tick_count -= button->long_press_repeat_tick;
				}
			}
			button->press_tick_count += tick;
		}
	}
	button->last_state = button_state;
}




void button_group_scan_tick_handler(BUTTON_GROUP_STRUCT* button_group, uint16_t ticks)
{
    static uint8_t i;
    if(ticks > 0){
        for (i = 0; i < button_group->button_num; i++) {
			button_scan_tick_handler(&(button_group->button[i]), ticks);
		}
	}
}

void button_group_init(BUTTON_GROUP_STRUCT* button_group,
						uint8_t button_group_button_num,
						BUTTON_STRUCT* button_space)
{
	memset(button_space, 0, sizeof(BUTTON_STRUCT)*button_group_button_num);
	button_group->button_pointer = 0;
    button_group->button_num = button_group_button_num;
}

BUTTON_STRUCT* button_new(BUTTON_GROUP_STRUCT* button_group,
							uint8_t jitter_tick,
							uint8_t long_press_tick,
							uint8_t long_press_repeat_tick,
							uint8_t (*hal_read_button_state_callbakc_handler)(BUTTON_STRUCT* button),
							void (*press_callback_handler)(BUTTON_STRUCT* button),
							void (*release_callback_handler)(BUTTON_STRUCT* button),
							void (*long_press_callback_handler)(BUTTON_STRUCT* button))
{
    BUTTON_STRUCT* button;
   if(button_group->button_pointer < button_group->button_num){
        button = button_group->button;
		button[button_group->button_pointer].jitter_tick = jitter_tick;
		button[button_group->button_pointer].long_press_tick = long_press_tick;
		button[button_group->button_pointer].long_press_repeat_tick = long_press_repeat_tick;
		button[button_group->button_pointer].hal_read_button_state_callbakc = hal_read_button_state_callbakc_handler;
		button[button_group->button_pointer].press_callback = press_callback_handler;
		button[button_group->button_pointer].release_callback = release_callback_handler;
		button[button_group->button_pointer].long_press_callback = long_press_callback_handler;
		button[button_group->button_pointer].press_tick_count = 0;
		button[button_group->button_pointer].last_state = BUTTON_RELEASE;
        button = &(button[button_group->button_pointer]);
        return button;
    }
    else{
        return 0;
    }
}
